package zwp.example.demo1.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zwp14
 */
@Configuration
@Slf4j
public class RedisConfiguration {
//    @Bean
//    @Primary
//    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
//        log.info("开始创建Redis模板对象......");
//        RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<Object,Object>();
//        //设置Redis的连接对象
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        //设置Redis key的序列化器
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        return redisTemplate;
//    }
//
//    @Bean("redisTemplateDB1")
//    public RedisTemplate<Object,Object> redisTemplateDb1(RedisConnectionFactory connectionFactory){
//        log.info("开始创建Redis模板对象......");
//        JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) connectionFactory;
//        //设置Redis 数据库
//        jedisConnectionFactory.setDatabase(1);
//        // 需要调用此方法来应用数据库更改
//        jedisConnectionFactory.afterPropertiesSet();
//        return redisTemplate(jedisConnectionFactory);
//    }
//
//    @Bean
//    public LettuceConnectionFactory lettuceConnectionFactory2(){
//        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration("localhost", 6379);
//        configuration.setDatabase(2);
//        return new LettuceConnectionFactory(configuration);
//    }
//
//    @Bean("redisTemplateDB2")
//    public RedisTemplate<Object,Object> redisTemplateDb2(LettuceConnectionFactory lettuceConnectionFactory){
//        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(new StringRedisSerializer());
//        return redisTemplate;
//    }

    /**
     * 根据配置创建不同数据库的连接工厂
     *
     * @return 连接工厂1
     */
    @Bean("redisConnectionFactory1")
    public RedisConnectionFactory redisConnectionFactory1() {
        return createLettuceConnectionFactory(1);
    }

    /**
     * 根据配置创建不同数据库的连接工厂
     *
     * @return 连接工厂2
     */
    @Bean("redisConnectionFactory2")
    public RedisConnectionFactory redisConnectionFactory2() {
        return createLettuceConnectionFactory(2);
    }

    /**
     * 创建对应的 RedisTemplate1
     *
     * @param factory1 redis连接工厂1
     * @return RedisTemplate
     */
    @Bean("redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate1(@Qualifier("redisConnectionFactory1") RedisConnectionFactory factory1) {
        return creatRedisTemplate(factory1);
    }

    /**
     * 创建对应的 RedisTemplate
     *
     * @param factory2 redis连接工厂2
     * @return RedisTemplate
     */
    @Bean("redisTemplate2")
    public RedisTemplate<Object, Object> redisTemplate2(@Qualifier("redisConnectionFactory2") RedisConnectionFactory factory2) {
        return creatRedisTemplate(factory2);
    }

    /**
     * 辅助方法，创建Lettuce连接工厂，指定redis数据库
     *
     * @param database redis数据库索引
     * @return Lettuce连接工厂
     */
    public LettuceConnectionFactory createLettuceConnectionFactory(int database) {
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory("localhost", 6379);
        connectionFactory.setDatabase(database);
        connectionFactory.setPassword("123456");
        return connectionFactory;
    }

    /**
     * 辅助方法，创建RedisTemplate实例
     *
     * @param factory redis连接工厂
     * @return RedisTemplate实例
     */
    public RedisTemplate<Object, Object> creatRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }


    /**
     * 可根据需求动态切换redis数据库
     *
     * @param redisTemplate1 redis实例1
     * @param redisTemplate2 redis实例2
     * @return 可选redis实例Map集合
     */
    @Bean("databaseRedisTemplateMap")
    public Map<Integer, RedisTemplate<Object, Object>> databaseRedisTemplateMap(
            @Qualifier("redisTemplate") RedisTemplate<Object, Object> redisTemplate1,
            @Qualifier("redisTemplate2")RedisTemplate<Object, Object> redisTemplate2
    ) {
        Map<Integer, RedisTemplate<Object, Object>> map = new HashMap<>(8);
        map.put(1, redisTemplate1);
        map.put(2, redisTemplate2);
        return map;
    }
}
